package com.example.mybatisplus.web.controller;

import com.example.mybatisplus.model.domain.AdminInfo;
import com.example.mybatisplus.utils.ImageToBase64Util;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.UserInfoService;
import com.example.mybatisplus.model.domain.UserInfo;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2023-10-16
 * @version v1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/api/userInfo")
public class UserInfoController {

    private final Logger logger = LoggerFactory.getLogger( UserInfoController.class );
<<<<<<< HEAD
    @RequestMapping("upload")
    public JsonResponse fileUpload(@RequestParam("file") MultipartFile fileUpload, @RequestParam("stuId") Long stuId) throws IOException {
/*        //获取文件名
        String fileName = fileUpload.getOriginalFilename();
        //获取文件后缀名。也可以在这里添加判断语句，规定特定格式的图片才能上传，否则拒绝保存。
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //为了避免发生图片替换，这里使用了文件名重新生成
        fileName = UUID.randomUUID() + suffixName;*/

        String s = ImageToBase64Util.imgToBase64(fileUpload);

=======

    @RequestMapping("upload")
    public JsonResponse fileUpload(@RequestParam("file") MultipartFile fileUpload, @RequestParam("stuId") Long stuId) throws IOException {
        String s = ImageToBase64Util.imgToBase64(fileUpload);
>>>>>>> 8ad7a103145a2eba732b7bb55d4d5e38eb754b01
        UserInfo userInfo = new UserInfo();
        userInfo.setStuId(stuId);
        userInfo.setStuHead(fileUpload.getBytes());
        userInfoService.updateById(userInfo);

        return  JsonResponse.message(true,s);
    }

    /**
     * 描述：学生登录
     *
     */
    @RequestMapping("StuLogin")
    public JsonResponse stuLogin(String phone ,String password){
            UserInfo login=userInfoService.stuLogin(phone,password);
        List<UserInfo>  userInfo =  userInfoService.findStudentBy(phone);
        System.out.println(userInfo);
        System.out.println(login);
            if(login!=null){
                return JsonResponse.success(login);
            }
            else if(userInfo.isEmpty()){
                return  JsonResponse.message(false,"账号不存在");
            }
            else{
                return  JsonResponse.message(false,"账号密码不匹配");
            }


    }

    @Autowired
    private UserInfoService userInfoService;
    /**
     * 查询所有学生
     *
     */
    @RequestMapping("getAllStuUsers")
    public JsonResponse getAllUsers(){
        List<UserInfo> s=userInfoService.getAllStuUsers();
        return JsonResponse.success(s);
    }
    /**
     * 分页查询学生
     *
     */
    @RequestMapping("findUserByPageAndLimit")
    public JsonResponse findUserByPageAndLimit(Integer page ,Integer limit){
        List<UserInfo> s=userInfoService.findUserByPageAndLimit( page , limit);
        return JsonResponse.success(s);
    }
    /**
     * 添加学生
     *
     */
    @RequestMapping("addUser")
    @ResponseBody
    public JsonResponse addUser(UserInfo users){
         try {
             Integer s=userInfoService.addUser(users);
             return  JsonResponse.success(s);
         }catch (Exception e){
             return  JsonResponse.message(false,"学号重复!!!");
         }

    }
    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping("findStudentById")
    @ResponseBody
    public JsonResponse getById( Integer id) {
        UserInfo  userInfo =  userInfoService.getById(id);
        return JsonResponse.success(userInfo);
    }
    /**
     * 描述：根据学生信息模糊查询
     *
     */
    @RequestMapping("findStudentBy")
    @ResponseBody
    public JsonResponse findStudentBy( String select) {
        List<UserInfo>  userInfo =  userInfoService.findStudentBy(select);
        return JsonResponse.success(userInfo);
    }

    /**
     * 描述：根据学生id查询初访信息
     *
     */
    @RequestMapping("GetConsultInfo")
    @ResponseBody
    public JsonResponse findStudentConsultInfo(String id) {
        List<UserInfo>  userInfo =  userInfoService.findStudentConsultInfo(id);
        return JsonResponse.success(userInfo);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping("delteStuById")
    @ResponseBody
    public JsonResponse deleteById( Integer stu_id)  {
        userInfoService.removeById(stu_id);
        return JsonResponse.success(null);
    }
    /**
     * 描述：批量删除student信息
     *
     */
    @PostMapping("deleteStudents")
    @ResponseBody
    public JsonResponse deleteStudents( @RequestParam(value = "ids[]") String[]  ids) {
        for (int i = 0; i < ids.length; i++) {
            System.out.println(ids[i]);
        }
        Integer s=userInfoService.deleteStudents(ids);
        return  JsonResponse.success(s);
    }

    /**
    * 描述：根据Id 更新
    *
    */

    @RequestMapping("updateStudentById")
    @ResponseBody
    public JsonResponse updateUserInfo(UserInfo  userInfo) throws Exception {
        userInfoService.updateById(userInfo);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建UserInfo
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(UserInfo  userInfo) throws Exception {
        userInfoService.save(userInfo);
        return JsonResponse.success(null);
    }

}

